package cn.antho.product.controller;

import cn.antho.basic.util.LoginContext;
import cn.antho.org.domain.Employee;
import cn.antho.org.mapper.EmployeeMapper;
import cn.antho.org.service.IEmployeeService;
import cn.antho.product.service.IProductService;
import cn.antho.product.domain.Product;
import cn.antho.product.query.ProductQuery;
import cn.antho.basic.util.AjaxResult;
import cn.antho.basic.util.PageList;
import cn.antho.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        product.setShopId(employee.getShopId());
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.add(product);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query)
    {
        return productService.queryByPage(query);
    }

    /**
     * 查询服务详情
     * @param id
     * @return
     */
    @GetMapping("/getProductDetail/{id}")
    public Product getProductDetail(@PathVariable("id") Long id)
    {
        return productService.getProductDetail(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        productService.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids, HttpServletRequest request) throws Exception {
        productService.onsale(ids, request);
        return AjaxResult.me().success();
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        productService.offsale(ids);
        return AjaxResult.me().success();
    }



}
